from rest_framework import serializers
from backend.UserAdmin.models import User
from .models import Sample, CellType, Cell


class SampleSerializer(serializers.ModelSerializer):
    #自定义显示字段
    celltypeName  = serializers.SerializerMethodField(
        source='cell_type.name',
    )
    userName = serializers.SerializerMethodField(
        source='entry_user.username',
    )



    class Meta:
        model = Sample
        fields = '__all__'

    def get_celltypeName(self, obj):  # 方法名必须和字段名完全一致（包括大小写）
        return obj.cell_type.name if obj.cell_type else None
    def get_userName(self, obj):  # 方法名必须和字段名完全一致（包括大小写）
        return obj.entry_user.username if obj.entry_user else None

class CellSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cell
        fields = '__all__'



class CellTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = CellType
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'




